---! 邀请礼包错误码
local ERR_CODE_INVAILD      = 1 ---! 邀请码无效
local ERR_QUERY_FIALED      = 2 ---! 数据库查询失败
local ERR_CODE_NO_EXISTS    = 3 ---! 邀请码不存在
local ERR_DATA_EXCEPTION    = 4 ---! 数据异常
local ERR_GIFT_NO_FOUND     = 5 ---! 礼包未找到
local ERR_CODE_ALREADY_USED = 6 ---! 邀请码已使用
local ERR_CODE_TAKE_FAILED  = 7 ---! 邀请码登记失败

-------------------------------------------------------------------------------
---! 内部方法
-------------------------------------------------------------------------------
---! 给与道具奖励
local function do_bonus_item(pattern)
    local item_map = {}
    for _, group in ipairs(string.split(pattern, "^")) do repeat
        local item_id, item_count = string.splitv(group, "-")
        if not item_id or not item_count then
            break
        end

        item_id = tonumber(item_id)
        if item_id <= 0 then
            break
        end

        item_count = tonumber(item_count)
        if item_count <= 0 then
            break
        end

        item_map[item_id] = (item_map[item_id] or 0) + item_count
    until true end
    return item_map
end

---! 查找礼包配置
local function find_gift_config(giftid)
    local configs = IMPORT_D:query("gift")
    if not configs then
        return
    end

    local config = configs[giftid]
    if not config then
        return
    end

    return config
end

---! 给与礼包奖励
local function give_gift_reward(player, config)
    local reward = {}
    for _, desc in ipairs(string.split(config.content, ";")) do repeat
        local type, pattern = string.splitv(desc, ":")
        if not type or not pattern then
            break
        end

        if type == "item" then
            reward[type] = do_bonus_item(pattern)
        end
    until true end

    local result = {}
    for type, reward_map in pairs(reward) do
        if type == "item" then
            for item_id, item_count in pairs(reward_map) do
                player:add_currency(item_id, item_count)
                table.insert(result, { propId = item_id, propCount = item_count, })
            end
            ITEM_D:send_currency_info(player)
        end
    end
    return result
end

-------------------------------------------------------------------------------
---! 对外接口
-------------------------------------------------------------------------------
GIFT_D = {}

---! 打开邀请礼包
function GIFT_D:open_inivite_gift(player, invite_code)
    if #invite_code ~= 10 then
        ---! 非法验证码
        local result = {}
        result.error = ERR_CODE_INVAILD
        player:send_packet("MSGS2CInviteGiftOpen", result)
        return
    end

    local ok, data = FILE_D:read_common_data("invite_code", invite_code)
    if not ok then
        ---! 查询数据库失败
        local result = {}
        result.error = ERR_QUERY_FIALED
        player:send_packet("MSGS2CInviteGiftOpen", result)
        return
    end

    if not data then
        ---! 邀请码不存在
        local result = {}
        result.error = ERR_CODE_NO_EXISTS
        player:send_packet("MSGS2CInviteGiftOpen", result)
        return
    end

    local gift_id, apply_time = data.gift_id, data.apply_time
    if not gift_id or not apply_time then
        ---! 数据库数据异常
        local result = {}
        result.error = ERR_DATA_EXCEPTION
        player:send_packet("MSGS2CInviteGiftOpen", result)
        return
    end

    local config = find_gift_config(gift_id)
    if not config then
        ---! 礼包配置不存在
        local result = {}
        result.error = ERR_GIFT_NO_FOUND
        player:send_packet("MSGS2CInviteGiftOpen", result)
        return
    end

    if apply_time ~= 0 then
        ---! 邀请码已使用
        local result = {}
        result.error = ERR_CODE_ALREADY_USED
        player:send_packet("MSGS2CInviteGiftOpen", result)
        return
    end

    local ok = FILE_D:update_common_data("invite_code", invite_code, os.time())
    if not ok then
        ---! 邀请码登记失败
        local result = {}
        result.error = ERR_CODE_TAKE_FAILED
        player:send_packet("MSGS2CInviteGiftOpen", result)
        return
    end

    ---! 给与礼包奖励
    local reward = give_gift_reward(player, config)

    ---! 领取奖励成功
    local result = {}
    result.error = 0
    result.reward = reward
    player:send_packet("MSGS2CInviteGiftOpen", result)
end
